package fpt.edu.vn.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.classic.Session;

import fpt.edu.vn.util.HibernateUtil;

/* 
 * SystemDao.java
 *  
 * 0.1
 * 
 * 10/15/2012 
 *  
 * Copyright notice 
 *  
 * Modification Logs: 
 *   DATE        AUTHOR      DESCRIPTION 
 *  -------------------------------------------------------- 
 *  15-Oct-2012  ThuongND    Create new 
 */
public class SystemDao {
	@SuppressWarnings("unchecked")
	public List<Object> listTopicHide() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Object> topics = null;
		try {

			topics = (List<Object>) session
					.createQuery(
							"select t.title , u.email from Topics t, Users u where t.statusTopicId= 1 and t.topicTypeId = 1 and t.usersId = u.usersId and DATEDIFF(CURDATE() , date(t.renewDate)) >= 30")
					.list();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return topics;
	}

	// Update StatusTopicId
	public boolean updateStatusTopicId() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		boolean update = false;
		try {
			String hql = "update Topics t set t.statusTopicId = 2 where t.statusTopicId= 1 and t.topicTypeId = 1 and DATEDIFF(CURDATE() , date(t.renewDate)) >= 30";
			Query query = session.createQuery(hql);
			int rowCount = query.executeUpdate();
			if (rowCount < 1) {
			} else {
				update = true;
			}

		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		session.getTransaction().commit();
		return update;
	}
}
